//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.action;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.mdiy.bean.ModelJsonBean;
import net.mingsoft.mdiy.biz.IModelBiz;
import net.mingsoft.mdiy.constant.e.ModelCustomTypeEnum;
import net.mingsoft.mdiy.entity.ModelEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-自定义模块接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/mdiy/form"})
public class FormAction extends BaseAction {
    @Autowired
    private IModelBiz modelBiz;

    public FormAction() {
    }

    @ApiIgnore
    @GetMapping({"/index"})
    public String index(HttpServletResponse response, HttpServletRequest request) {
        return "/mdiy/form/index";
    }

    @ApiOperation("查询自定义模型列表接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "modelName",
            value = "模型名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "modelTableName",
            value = "模型表名",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appId",
            value = "应用编号",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "modelJson",
            value = "json",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "createBy",
            value = "创建人",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "createDate",
            value = "创建时间",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "updateBy",
            value = "修改人",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "updateDate",
            value = "修改时间",
            required = false,
            paramType = "query"
    )})
    @GetMapping({"/list"})
    @RequiresPermissions({"mdiy:form:view"})
    @ResponseBody
    public ResultData list(@ModelAttribute @ApiIgnore ModelEntity modelEntity, HttpServletResponse response, HttpServletRequest request) {
        modelEntity.setModelCustomType(ModelCustomTypeEnum.FORM.getLabel());
        BasicUtil.startPage();
        List modelList = this.modelBiz.list(new QueryWrapper(modelEntity));
        return ResultData.build().success(new EUListBean(modelList, (int)BasicUtil.endPage(modelList).getTotal()));
    }

    @ApiOperation("查询自定义模型接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "modelName",
            value = "模型名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "modelTableName",
            value = "模型表名",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "appId",
            value = "应用编号",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "modelJson",
            value = "json",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "createBy",
            value = "创建人",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "createDate",
            value = "创建时间",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "updateBy",
            value = "修改人",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "updateDate",
            value = "修改时间",
            required = false,
            paramType = "query"
    )})
    @GetMapping({"/get"})
    @ResponseBody
    public ResultData get(ModelEntity modelEntity, HttpServletResponse response, HttpServletRequest request) {
        if (modelEntity != null && !StringUtils.isEmpty(modelEntity.getModelName())) {
            modelEntity.setModelCustomType(ModelCustomTypeEnum.FORM.getLabel());
            ModelEntity model = (ModelEntity)this.modelBiz.getOne(new QueryWrapper(modelEntity));
            return !this.getPermissions("mdiy:form:view", "mdiy:formData:" + model.getModelName() + ":view") ? ResultData.build().error("没有权限!") : ResultData.build().success(model);
        } else {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("model.name")}));
        }
    }

    @ApiOperation("导入自定义模型")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "modelJson",
            value = "json",
            required = true,
            paramType = "query"
    )})
    @LogAnn(
            title = "导入",
            businessType = BusinessTypeEnum.INSERT
    )
    @PostMapping({"/importJson"})
    @ResponseBody
    @RequiresPermissions({"mdiy:form:importJson"})
    public ResultData importJson(@ModelAttribute @ApiIgnore ModelEntity modelEntity, HttpServletResponse response, HttpServletRequest request, BindingResult result) {
        if (StringUtils.isBlank(modelEntity.getModelJson())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.json")}));
        } else {
            new ModelJsonBean();

            ModelJsonBean modelJsonBean;
            try {
                modelJsonBean = (ModelJsonBean)JSONUtil.toBean(modelEntity.getModelJson(), ModelJsonBean.class);
            } catch (Exception var7) {
                return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("model.json")}));
            }

            return this.modelBiz.importConfig(ModelCustomTypeEnum.FORM.getLabel(), modelJsonBean) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.exist", new String[]{this.getResString("table.name")}));
        }
    }

    @ApiOperation("更新导入自定义模型")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "modelJson",
            value = "json",
            required = true,
            paramType = "query"
    )})
    @LogAnn(
            title = "导入",
            businessType = BusinessTypeEnum.INSERT
    )
    @PostMapping({"/updateJson"})
    @ResponseBody
    @RequiresPermissions({"mdiy:form:update"})
    public ResultData updateJson(@ModelAttribute @ApiIgnore ModelEntity modelEntity, HttpServletResponse response, HttpServletRequest request, BindingResult result) {
        if (StringUtils.isBlank(modelEntity.getModelJson())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.json")}));
        } else if (StringUtils.isBlank(modelEntity.getId())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("id")}));
        } else {
            new ModelJsonBean();

            ModelJsonBean modelJsonBean;
            try {
                modelJsonBean = (ModelJsonBean)JSONUtil.toBean(modelEntity.getModelJson(), ModelJsonBean.class);
            } catch (Exception var7) {
                return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("model.json")}));
            }

            return this.modelBiz.updateConfig(modelEntity.getId(), modelJsonBean) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.exist", new String[]{this.getResString("table.name")}));
        }
    }

    @ApiOperation("批量删除自定义模型列表接口")
    @LogAnn(
            title = "批量删除自定义模型列表接口",
            businessType = BusinessTypeEnum.DELETE
    )
    @PostMapping({"/delete"})
    @ResponseBody
    @RequiresPermissions({"mdiy:form:del"})
    public ResultData delete(@RequestBody List<ModelEntity> models, HttpServletResponse response, HttpServletRequest request) {
        List<String> ids = (List)models.stream().map((p) -> {
            return p.getId();
        }).collect(Collectors.toList());
        return this.modelBiz.delete(ids) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("id")}));
    }
}
